package main.java.com.web;

import main.java.com.entity.Employee;
import main.java.com.service.LoginService;
import main.java.com.service.impl.LoginServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * 后台登录控制器
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");

        String name = req.getParameter("username");
        String password = req.getParameter("password");

        LoginService loginService = new LoginServiceImpl();
        List<Employee> list = loginService.login(name, password);
        if (list.size() == 0) {
            req.setAttribute("msg", "用户名或密码错误");
            req.getRequestDispatcher("/backend/login.jsp").forward(req, resp);
        } else {
            //过滤器拦截
            HttpSession session = req.getSession();
            session.setAttribute("htuser", name);
            req.getSession().setAttribute("list", list);
            req.getRequestDispatcher("/findEmployeeByPageServlet").forward(req, resp);
        }

    }



}
